package com.yyx.demo.publishSubscribe.test;

import com.yyx.demo.lock.test.LockTest;
import com.yyx.demo.publishSubscribe.PublishSubscriber;
import org.apache.zookeeper.ZooKeeper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Random;

public class testPublisher {

    final static Logger log = LoggerFactory.getLogger(testPublisher.class);

    public static final String zkServerPath = "10.2.132.171:2181";
    public static final Integer timeout = 50000;

    private ZooKeeper createClient() throws IOException {
        ZooKeeper zk = new ZooKeeper(zkServerPath, timeout, new LockTest());
        return zk;
    }

    public static void main(String[] args) throws IOException {
        testPublisher testPublisher=new testPublisher();
        ZooKeeper zooKeeper=testPublisher.createClient();
        try {
            PublishSubscriber publishSubscriber=new PublishSubscriber(zooKeeper,null);
            Random r = new Random(1);

            while (true){
                int ran1 = r.nextInt(10);
                System.out.println("发布了:"+ran1);
                String s= String.valueOf(ran1);
                publishSubscriber.PublishEvent(s.getBytes(),"/test",null);
                System.out.println("阻塞8s");
                Thread.sleep(8*1000);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
